package com.hqyj.system.controller;

import com.hqyj.system.model.Stock;
import com.hqyj.system.model.User;
import frame.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/stockController")
public class StockController extends BaseController {
    @RequestMapping("/list.do")
    public String List(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, Stock stock) {
        //1.接收数据;
        //3.调用业务;
        List<Stock> stock_dbList = stockService.queryAllStock();
        //返回数据
        model.addAttribute("stock_dbList", stock_dbList);
        //2.跳转页面;
        return "view/system/stock/stockList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, Stock stock) {
        //1.接收数据;
        //3.调用业务;
        int n = stockService.addStockByStock(stock);
        List<Stock> stock_dbList = stockService.queryAllStock();
        //返回数据
        model.addAttribute("stock_dbList", stock_dbList);
        //2.跳转页面;
        return "view/system/stock/stockList";
    }
    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, String stockIdStr,Stock stock) {
        //1.接收数据;

        //3.调用业务;
        //字符类型转成数字类型
        int stockId = Integer.parseInt(stockIdStr);
        int n = stockService.deleteStockrBystockId(stockId);

        //2.跳转页面;
        List<Stock> stock_dbList = stockService.queryAllStock();
        model.addAttribute("stock_dbList", stock_dbList);
        return "view/system/stock/stockList";
    }
    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, String stockIdStr) {
        //1.接收数据;
        //字符类型转成数字类型
        int stockId = Integer.parseInt(stockIdStr);

        //3.调用业务;
        Stock stock_db = stockService.queryStockByStockId(stockId);

        model.addAttribute("stock_db", stock_db);

        //2.跳转页面;
        return "view/system/stock/updateStock";
    }
    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, String stockIdStr , Stock stock) {
        //1.接收数据;
        //字符类型转成数字类型


        //3.调用业务;
        int n = stockService.updateStockByStock(stock);

        //2.跳转页面;
        List<Stock> stock_dbList = stockService.queryAllStock();
        model.addAttribute("stock_dbList", stock_dbList);
        return "forward:/stockController/list.do";
    }

}
